/**
 * Created by chenshengli on 2015/7/1 0001.
 */
define([], function () {
    return function ($scope, $rootScope, $location, $routeParams, $http, $resourceService) {

        $scope.host = {};
        $scope.addHost = function () {
            var formValidate = $('.ui.form').form('is valid');
            if (!formValidate)return;
            console.log($scope.host);
            var host = $scope.host;
            $http({
                method: "PUT", url: "api/managers/host", data: $scope.host
            }).success(function (data, status) {
                if(data.status == "ok"){
                    alert("环境新增成功~");
                    window.location.reload();
                }else{
                    alert("环境新增失败~");
                }
            }).error(function (data, status) {
                alert("环境新增异常~");
            })
        }



        $scope.chooseParentHost = function(hostId){
            var hostIp = $('#'+hostId).find('.header').html();
            console.log(hostIp);
            $('#hostParent').val(hostIp);
            //$scope.host.hostParent = hostIp;
        }

        $scope.showHostParentSelect = function (obj) {
            $('.ui.modal').modal('show');
            $(obj.target).trigger("blur");
        }

        $resourceService.queryEnvs($http, $scope);
        $resourceService.queryHosts($http, $scope);

        $('.menu .item').tab();
        $('#hostEnvSelect').dropdown({
            onChange: function (value,text ) {
                $scope.host.hostEnv = value;
            }
        });

        $('#hostParentSelect').dropdown({
            onChange: function (value,text) {
                $scope.host.hostParent = value;
            }
        });
        $('#hostOSSelect').dropdown({
            onChange: function (value,text) {
                $scope.host.hostOS = value;
            }
        });
        $('#hostTypeSelect').dropdown({
            onChange: function (value,text) {
                $scope.host.hostType = value;
            }
        });
        var validationRules = {
            hostIp: {
                identifier: 'hostIp',
                rules: [
                    {
                        type: 'empty',
                        prompt: 'Please enter a hostIp'
                    }
                ]
            },
            hostUser: {
                identifier: 'hostUser',
                rules: [
                    {
                        type: 'empty',
                        prompt: 'Please enter a hostUser'
                    }
                ]
            }, hostUserPassword: {
                identifier: 'hostUserPassword',
                rules: [
                    {
                        type: 'empty',
                        prompt: 'Please enter a hostUserPassword'
                    }
                ]
            }, hostEnv: {
                identifier: 'hostEnv',
                rules: [
                    {
                        type: 'empty',
                        prompt: 'Please enter a hostEnv'
                    }
                ]
            }, hostOS: {
                identifier: 'hostOS',
                rules: [
                    {
                        type: 'empty',
                        prompt: 'Please enter a hostOS'
                    }
                ]
            }
            , hostType: {
                identifier: 'hostType',
                rules: [
                    {
                        type: 'empty',
                        prompt: 'Please enter a hostType'
                    }
                ]
            }
        }

        $('.ui.form').form({
            fields: validationRules,
            inline: true
        });
    }
});